home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP6.TXT < prev    next >
Text File  |  1987-07-04  |  7KB  |  190 lines

  1.                        Chapter 6 - Defines and Macros
  2.  
  3.  
  4.               DEFINES AND MACROS ARE AIDS TO CLEAR PROGRAMMING
  5.  
  6.              Load and display the file named DEFINE.C for your first
  7.         look  at  some defines and macros.   Notice the  first  four
  8.         lines  of the program each starting with the word "#define".
  9.         This is the way all defines and macros are defined.   Before
  10.         the actual compilation starts,  the compiler goes through  a
  11.         preprocessor  pass  to resolve all of the defines.   In  the
  12.         present case,  it will find every place in the program where
  13.         the combination "START" is found and it will simply  replace
  14.         it  with the 0 since that is the definition.   The  compiler
  15.         itself  will  never see the word "START",  so as far as  the
  16.         compiler  is concerned,  the zeros were  always  there.   It
  17.         should  be clear to you by now that putting the word "START"
  18.         in  your  program  instead  of  the  numeral  0  is  only  a
  19.         convenience  to you and actually acts like a  comment  since
  20.         the  word  "START" helps you to understand what the zero  is
  21.         used for.
  22.  
  23.              In  the  case of a very small  program,  such  as  that
  24.         before  you,  it  doesn't really matter what you  use.   If,
  25.         however,  you  had  a 2000 line program before you  with  27
  26.         references to the START,  it would be a completely different
  27.         matter.   If  you wanted to change all of the STARTs in  the
  28.         program  to a new number,  it would be simple to change  the
  29.         one  #define,  but difficult to find and change all  of  the
  30.         references  to it manually,  and possibly disastrous if  you
  31.         missed one or two of the references.
  32.  
  33.              In  the  same manner,  the preprocessor will  find  all
  34.         occurrences of the word "ENDING" and change them to 9,  then
  35.         the  compiler  will  operate  on the changed  file  with  no
  36.         knowledge that "ENDING" ever existed.
  37.  
  38.              It is a fairly common practice in C programming to  use
  39.         all  capital letters for a symbolic constant such as "START"
  40.         and  "ENDING"  and use all lower case letters  for  variable
  41.         names.  You can use any method you choose since it is mostly
  42.         a matter of personal taste.
  43.  
  44.                            IS THIS REALLY USEFUL?
  45.  
  46.              When  we  get  to the  chapters  discussing  input  and
  47.         output,  we  will need an indicator to tell us when we reach
  48.         the end-of-file of an input file.  Since different compilers
  49.         use different numerical values for this,  although most  use
  50.         either a zero or a minus 1, we will write the program with a
  51.         "define"  to define the EOF used by our particular compiler.
  52.         If at some later date,  we change to a new compiler, it is a
  53.         simple matter to change this one "define" to fix the  entire
  54.         program.  In Turbo C, the EOF is defined in the STDIO.H file
  55.  
  56.  
  57.                                   Page 43
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                        Chapter 6 - Defines and Macros
  68.  
  69.  
  70.         on  line 44.  You can observe this for yourself  by  listing
  71.         this  file.
  72.  
  73.                               WHAT IS A MACRO?
  74.  
  75.              A macro is nothing more than another define,  but since
  76.         it  is capable of at least appearing to perform some logical
  77.         decisions  or  some math functions,  it has a  unique  name.
  78.         Consider the third line of the program on your screen for an
  79.         example of a macro.   In this case, anytime the preprocessor
  80.         finds the word "MAX" followed by a group in parentheses,  it
  81.         expects  to find two terms in the parentheses and will do  a
  82.         replacement of the terms into the second  definition.   Thus
  83.         the  first  term  will  replace  every  "A"  in  the  second
  84.         definition and the second term will replace every "B" in the
  85.         second definition.   When line 12 of the program is reached,
  86.         "index" will be substituted for every "A",  and "count" will
  87.         be  substituted  for  every "B".   Remembering  the  cryptic
  88.         construct  we studied a couple of chapters ago  will  reveal
  89.         that  "mx"  will  receive the maximum value  of  "index"  or
  90.         "count".   In  like manner,  the "MIN" macro will result  in
  91.         "mn" receiving the minimum value of "index" or "count".  The
  92.         results are then printed out.   There are a lot of seemingly
  93.         extra  parentheses in the macro definition but they are  not
  94.         extra,  they  are  essential.   We  will discuss  the  extra
  95.         parentheses in our next program.
  96.  
  97.              Compile and run DEFINE.C.
  98.  
  99.                          LETS LOOK AT A WRONG MACRO
  100.  
  101.              Load  the  file named MACRO.C and display  it  on  your
  102.         screen for a better look at a macro and its use.   The first
  103.         line  defines a macro named "WRONG" that appears to get  the
  104.         cube of "A",  and indeed it does in some cases, but it fails
  105.         miserably in others.  The second macro named "CUBE" actually
  106.         does get the cube in all cases.
  107.  
  108.              Consider  the program itself where the CUBE of i+offset
  109.         is  calculated.   If  i is 1,  which it is  the  first  time
  110.         through,  then  we will be looking for the cube of 1+5 =  6,
  111.         which  will result in 216.  When using "CUBE",  we group the
  112.         values like this, (1+5)*(1+5)*(1+5) = 6*6*6 = 216.  However,
  113.         when we use WRONG,  we group them as 1+5*1+5*1+5 = 1+5+5+5 =
  114.         16 which is a wrong answer.   The parentheses are  therefore
  115.         required  to  properly  group the  variables  together.   It
  116.         should  be clear to you that either "CUBE" or "WRONG"  would
  117.         arrive  at  a correct answer for a single  term  replacement
  118.         such as we did in the last program.   The correct values  of
  119.         the  cube  and the square of the numbers are printed out  as
  120.         well as the wrong values for your inspection.
  121.  
  122.  
  123.                                   Page 44
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                        Chapter 6 - Defines and Macros
  134.  
  135.  
  136.  
  137.              The remainder of the program is simple and will be left
  138.         to your inspection and understanding.
  139.  
  140.  
  141.         PROGRAMMING EXERCISE
  142.  
  143.         1.   Write a program to count from 7 to -5 by counting down.
  144.              Use #define statements to define the limits. (Hint, you
  145.              will  need to use a decrementing variable in the  third
  146.              part of the "for" loop control.
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                   Page 45
  190.